<?php

namespace app\services;

use app\payment\PayFactory;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserPropertyLog;
use plugin\admin\app\model\UserTeam;
use support\Log;
use Webman\RedisQueue\Redis;

class ModelService
{
    public function bindParent($order)
    {
        try {
            if (empty($order['invite_user_id'])) {
                return;
            }


            $parentInfo = User::where('id', $order['invite_user_id'])->first();

            if ($parentInfo['level'] == 1) {
                return;
            }

            $userInfo = User::where('id', $order['user_id'])->first();

            if ($userInfo['parent_id']) {
                return;
            }

            $teamInfo = UserTeam::where('user_id', $order['invite_user_id'])->first();

            $limitCount = ConfigService::get('recommend_count_limit');

            if ($teamInfo['recommend_count'] + 1 > $limitCount) {
                return;
            }

            UserService::bindParent($order['user_id'], $order['invite_user_id']);

            $parents = UpLineService::get($order['user_id']);

            foreach ($parents as $parent) {
                $nextLevel = UserLevelService::levelUpCheck($parent);

                if ($nextLevel) {
                    UserLevelService::levelUp($parent, $nextLevel);
                }
            }

        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function toVip($order)
    {
        try {
            UserService::upgradeToVip($order['user_id']);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function reduceIntegral($order)
    {
        try {
            if ($order['integral'] > 0) {
                PropertyService::reduce($order['user_id'], PayFactory::INTEGRAL, $order['integral'], UserPropertyLog::INTEGRAL_PAY_ORDER, '', $order['order_no']);
            }
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }

    }

    public function profitSharing($order)
    {
        try {
            OrderService::rebate($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function updateTeamInfo($order)
    {
        try {
            UserTeamService::addOrderCount($order);
        } catch (\Exception $e) {
            Log::notice($e->getMessage());
            if (getenv('APP_DEBUG')) {
                throw $e;
            }
        }
    }

    public function checkCanCreateOrder($orderPreviewInfo)
    {
        $limitCount = ConfigService::get('limit_gift_count');

        if (OrderService::getRangeTimeGiftOrderCount(date('Y-m-01 00:00:00'), date('Y-m-t 23:59:59')) + 1 > $limitCount) {
            throw new \Exception('每月仅能下单一次报单产品');
        }
    }
}